import {
  api,
  util,
  forms,
  storage
} from "../util";
import {
  router
} from '../router';
import {
  getTabbar
} from "../newUtil";
import authorize from './authorize.js'
import { getStorageSync, setStorageSync } from "../compatible";
var app = getApp();
const profile = {
  getIndexCount: function (callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      FromFKId: entity.fkId,
      FromFKFlag: entity.fkFlag,
      ProprietorId: app.dns.proprietorId,
    };
    util.post("order.order.count", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res);
          }
        }
      }, "2.0", null, false);
  },
  getRetreatCount: function (callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      ProprietorId: app.dns.proprietorId,
      Token: entity.manager.Token,
    };
    util.post("store.order.counts", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res);
          }
        }
      }, "2.0", null, false);
  },
  /**
   * 加载微信小程序码
   */
  load_wx_qrcode: function (currentRoute, callback) {
    var entity = authorize.getEntity();
    var param = {
      fkway: app.dns.fkWay,
      path: currentRoute.url,
      scene: currentRoute.urlWithArgs,
      width: 430,
      proprietorId: app.dns.proprietorId,
      fkId: app.dns.proprietorId,
      fkFlag: app.dns.proprietor
    };
    util.post("social.minipro.get.qr", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          if (callback != undefined && typeof (callback) == "function") {
            callback("");
          }
        }

      }, "3.0");
  },
  /**
   * 加载微信帐号
   */
  load_wx_account: function (callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      FKFlag: entity.fkFlag,
      FKId: entity.fkId,
      UserId: entity.manager.ManagerId
    };
    util.post("store.authbind.list", param,
      function (res) {
        res = res.data;
        if (callback != undefined && typeof (callback) == "function") {
          callback(res.Content);
        }
      }, "3.0");
  },
  /**
   * 设置是否接受消息
   */
  set_is_receivemsg: function (id, callback) {
    var entity = authorize.getEntity();
    var param = {
      Id: id,
      Token: entity.manager.Token,
      FKFlag: entity.fkFlag,
      FKId: entity.fkId,
      UserId: entity.manager.ManagerId
    };
    util.post("store.authbind.SetIsReceiveMsg", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.alert(res.Message);
        }
      }, "3.0", null, false);
  },
  /**
   * 保存设置
   * @param {Object} data 用户信息
   * @param {String} profileToken 用户临时token（需要完善信息时返回的token）
   * @callback 用临时token修改信息后的回调方法
   */
  save_setting: function (data, profileToken, callback) {
    var that = this;
    var bo = forms.formValidate(data);
    if (!bo) return;
    var data = forms.getValues(data);

    var entity = authorize.getEntity() || {};
    let params = {}
    if (profileToken) {
      params = util.mergeJsonObject(data, {
        profileToken,
        CoverKeyword: "StoreCover"
      });
    } else {
      params = util.mergeJsonObject(data, {
        FKId: entity.fkId,
        FKFlag: entity.fkFlag,
        Id: entity.fkId,
        CoverKeyword: "StoreCover"
      });
    }

    util.post("store.shop.edit", params,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (profileToken) {
            if (callback) {
              callback()
            }
            return
          }
          util.alert("保存成功", "修改配置", function () {
            entity.role = util.mergeJsonObject(entity.role, data);
            authorize.setCookie("store", {
              role: entity.role,
              manager: entity.manager
            });
            setStorageSync('tabindex', app.dns.centerNum)
            router.switchTab({
              path: "center"
            });
          });
        } else {
          util.alert("保存失败:" + res.Message);
        }
      }, "3.0", null, false);
  },
  /**
   * 通过临时token修改用户名称
   * @param {String} name 修改的用户名
   * @param {String} ProfileToken 登录时的临时token
   * @param {String} userId 用户登录成功后的用户Id
   */
  editUserName: function (name = '', ProfileToken = '', userId = '') {
    let params = {
      Name: name
    }
    if (userId) {
      params.Id = userId
    } else if (ProfileToken) {
      params.ProfileToken = ProfileToken
    }
    return new Promise((resolve, reject) => {
      util.post("store.shop.updatename", params,
      function (res) {
        res = res.data || {}
        if (res.Success) {
          resolve(res)
        } else {
          reject(res)
        }
      }, "3.0", null, false)
    })
  },
  /**
   * 获得主管理员手机号
   */
  get_initiator_mobile: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      FKId: entity.fkId,
      FKFlag: entity.fkFlag
    };
    util.post("account.store.get.initiator.mobile", param,
      function (res) {
        res = res.data;

        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.alert(res.Message);
        }

      }, "3.0");
  },
  /**
   * 不包含的菜单
   */
  get_no_contain_menu: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      proprietorId: app.dns.proprietorId
    };
    util.post("account.store.menu.uncontain", param,
      function (res) {
        res = res.data;
        if (callback != undefined && typeof (callback) == "function") {
          callback(res);
        }
      }, "3.0");
  },
  /*
   * 业务员显示
   */
  salesman: function (callback) {
    var entity = authorize.getEntity() || {};
    var param = {
      StoreId: entity.fkId,
      ProprietorId: app.dns.proprietorId
    };
    util.post("store.customer.isshowlinkmanbutton", param, function (res) {

      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.0");
  },
  /**
   * 退出登录
   */
  logOut: function (callback) {
    if (!authorize.isLogin('store')) {
      return;
    }
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("store.logout", param, function (res) {
      res = res.data;
      util.removeCategoryCache()
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.1");
  },
  loginOut: function () {
    let tabbar = getTabbar()
    tabbar.setTabBarBadge(0)
    app.setTabBarBadge(0);
    authorize.loginOut();
    app.globalData.index.isReload = true;
    if (app.dns.fkId != app.dns.proprietorId) {
      app.get_domain_info();
    }
  }
}
/**
 * 财务相关
 */
const finance = {
  get_account_list: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      UserId: 0
    };
    util.post("Finance.cashaccount.list", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          if (callback != undefined && typeof (callback) == "function") {
            callback(res.Content);
          }
        } else {
          util.alert("操作失败:" + res.Message);
        }
      }, "3.0");
  },
  /**
   * 获得预存款信息
   */
  get_prestore_info: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      ProprietorId: app.dns.proprietorId,
      FKWay: app.dns.fkWay
    };
    util.fetch("finance.prestore.get", param, callback, "3.0");
  },
  /**
   * 获得预存款明细
   */
  get_prestore_detail: (data, callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      ProprietorId: app.dns.proprietorId,
      FKWay: app.dns.fkWay,
      PageIndex: data.PageIndex,
      PageSize: data.PageSize,
    };
    util.post("finance.prestore.page", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          var resData = res.Content;
          if (callback != undefined && typeof (callback) == "function") {
            callback(resData);
          }

        }
      }, "3.0", null, false);
  },
  /**
   * 获取充值奖励配置
   */
  get_recharge_reward_config: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      FKId: app.dns.proprietorId,
      FKFlag: app.dns.proprietor,
      ProprietorId: app.dns.proprietorId,
      FKWay: app.dns.fkWay,
      UserId: entity.fkId
    };
    util.post("finance.recharge.reward", param,
      function (res) {
        res = res.data;
        if (res.Success) {
          var rewards = res.Content.data;
          var fixRewards = [];
          var rateRewards = [];
          for (var i = 0, len = rewards.length; i < len; i++) {
            var re = rewards[i];
            if (re.RewardType == 1) {
              rateRewards.push(re);
            } else {
              fixRewards.push(re);
            }
          }
          if (callback != undefined && typeof (callback) == "function") {
            callback(rewards, fixRewards, rateRewards);
          }

        }
      }, "3.1", null, false);
  },
  /**
   * 计算奖励
   */
  caculate_reward: (recharge, rewards, chargeType = '') => {
    var reward = 0; //奖励
    for (var i = 0, len = rewards.length; i < len; i++) {
      var re = rewards[i];
      if (re.RewardType == 0) {
        if (recharge == parseFloat(re.BaseAmount)) {
          reward = parseFloat(re.Rewards);
          break;
        }
      } else {
        if (recharge >= parseFloat(re.BaseAmount) && (recharge <= parseFloat(re.MaxAmount) || parseFloat(re.MaxAmount) == 0)) {
          reward = parseFloat(recharge * parseFloat(re.Rewards) / 100, 2, true);
        }
      }
    }
    return {
      finalrecharge: recharge + reward,
      rewardDesc: reward > 0 ? "（奖励" + reward.toFixed(2) + "元） " : "",
      reward: reward.toFixed(2)
    }
  },
  /**
   * 去充值
   * @currentRecharge 金额
   * @reward 奖励
   */
  goRecharge: (amount, reward) => {
    var entity = authorize.getEntity();
    var param = {
      FKFlag: entity.fkFlag,
      Amount: amount,
      ProprietorId: app.dns.proprietorId
    };
    util.post("finance.reward.amount", param,
      function (res) {
        var res = res.data;
        if (parseFloat(res.Content) != reward && reward > 0) {
          util.alert("充值奖励金额发生变化!请重新发起.");
          return;
        } else {
          var realReward = parseFloat(res.Content);
          finance.rechargePay(realReward, amount);
        }
      }, "3.0", null, false);
  },
  /**
   * 充值接口
   * @realReward 真实的奖励金额
   * @rechargeAmount 充值金额 
   *  */
  rechargePay: (realReward, rechargeAmount) => {
    var that = this;
    var entity = authorize.getEntity();
    var content = `
    <PayTypeId>2</PayTypeId>
    <IsMergePay>0</IsMergePay>
    <PayFKFlag>${entity.fkFlag}</PayFKFlag>
    <PayFKId>${entity.fkId}</PayFKId>
    <ReceiveFKFlag>2</ReceiveFKFlag>
    <ReceiveFKId>${app.dns.proprietorId}</ReceiveFKId>
    <SponsorTypeId>1</SponsorTypeId>
    <SponsorIds>${realReward}</SponsorIds>
    <SponsorNumber>${util.getCurrentTimeStamp()}</SponsorNumber>
    <Payables>${rechargeAmount}</Payables>
    <IsPay>1</IsPay>
    <IsUpdate>0</IsUpdate>`;
    var param = {
      Token: entity.manager.Token,
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      Type: 1,
      DataType: 0,
      Terminal: 5,
      Content: content,
      amount: rechargeAmount,
      rewards: realReward,
      ProprietorId: app.dns.proprietorId
    };
    util.post("payment.cashier.passport", param,
      function (res) {
        res = res.data;
        if (!res.Success) {
          util.alert(res.Message);
          return;
        } else {
          var o = res.Content;
          //跳转到支付页面
          router.redirect({
            path: "cashier",
            params: {
              paytoken: o.recordToken
            }
          });

        }
      }, "3.0", null, false);
  },
  /**
   * 获得自已的积分
   */
  get_self_integral: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      StoreId: entity.fkId,
      ProprietorId: app.dns.proprietorId
    };
    util.fetch("store.integral.get", param, callback, "3.0");
  },
  /**
   * 加载积分数据
   */
  loadIntegralList: (page, callback) => {
    var entity = authorize.getEntity();
    var param = util.mergeJsonObject(page, {
      FKId: entity.fkId
    });
    util.post("store.integral.list", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Content);
      }
    }, "3.0");
  },
  /**
   * 签到奖励积分
   */
  checkInIntegral: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("integral.checkInInfo.checkIn", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.0");
  },
  getCheckInInfo: (callback) => {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      ProprietorId: app.dns.proprietorId
    };
    util.post("integral.checkInInfo.get", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.0");
  },
  getBankAccounts(callback) {
    var entity = authorize.getEntity();
    var param = {
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      Token: entity.manager.Token,
      UserId: 0,
      ProprietorId: app.dns.proprietorId
    };
    util.post("finance.bankcode.list", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Content);
      }
    }, "3.0");
  },
  /**
   * 添加帐号
   */
  onAddAccount(data, callback) {
    var that = this;
    var bo = forms.formValidate(data);
    if (!bo) return;
    var data = forms.getValues(data);
    var entity = authorize.getEntity();
    data.FKId = entity.fkId;
    data.FKFlag = entity.fkFlag;
    data.Token = entity.manager.Token;
    util.post("finance.cashaccount.add", data, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          data.Id = res.Content;
          callback(data);
        }
      } else {
        util.tips("添加失败：" + res.Message);
      }
    }, "3.0", null, false);
  },
  /**
   * 收款账户
   */
  getAccountList(data, callback) {
    var entity = authorize.getEntity();
    var param = {
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      Token: entity.manager.Token,
      StoreId: entity.fkId,
      Status: data.status,
      Keyword: data.keyword,
      ProprietorId: app.dns.proprietorId,
      PageIndex: data.PageIndex,
      PageSize: data.PageSize
    };
    util.post("finance.cashaccount.page", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Content);
      }
    }, "3.1");
  },
  /**
   * 账户详情数据
   */
  getAccountDetail(data, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Id: data.id
    };
    util.post("finance.cashaccount.get", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Content);
      }
    }, "3.1");
  },
  /**
   * 添加帐号
   */
  bindAddAccount(data, callback) {
    var that = this;
    var bo = forms.formValidate(data);
    if (!bo) return;
    var data = forms.getValues(data);
    var params = {};
    if (Number(data.AccountType) != 1) {
      params = {
        AccountType: data.AccountType,
        Account: data.Account,
        Name: data.Name,
        IsDefault: data.IsDefault,
        Status: data.Status,
        ImgSrc: data.ImgSrc
      };
    } else {
      params = data;
    }
    var entity = authorize.getEntity();
    params.FKId = entity.fkId;
    params.FKFlag = entity.fkFlag;
    params.Token = entity.manager.Token;
    params.StoreId = entity.fkId;
    // return;
    util.post("finance.cashaccount.add", params, function (res) {
      res = res.data;
      if (res.Success) {
        util.tips('保存成功');
        if (callback != undefined && typeof (callback) == "function") {
          data.Id = res.Content;
          callback(data);
        }
      } else {
        util.tips("添加失败：" + res.Message);
      }
    }, "3.1", null, false);
  },
  //申请退换货的时候调用的---
  getAddAccount(data, callback) {
    var entity = authorize.getEntity();
    var params = {
      Token: entity.manager.Token,
      FKId: entity.fkId,
      FKFlag: entity.fkFlag,
      StoreId: entity.fkId,
      AccountType: data.AccountType,
      Account: data.Account,
      IsDefault: data.IsDefault,
      ImgSrc: data.ImgSrc,
      Name: data.Name,
      Status: data.Status,
    };
    // return;
    util.post("finance.cashaccount.add", params, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          data.Id = res.Content;
          callback(data);
        }
      } else {
        util.tips("添加失败：" + res.Message);
      }
    }, "3.1", null, false);
  },
  /**
   * 编辑帐号
   */
  bindEditAccount(data, types, callback) {
    var that = this;
    var bo = forms.formValidate(data);
    if (!bo) return;
    var data = forms.getValues(data);
    var params = {};
    if (Number(data.AccountType) != 1) {
      params = {
        AccountType: data.AccountType,
        Account: data.Account,
        Name: data.Name,
        IsDefault: data.IsDefault,
        Status: data.Status,
        ImgSrc: data.ImgSrc
      };
    } else {
      params = data;
    }
    var entity = authorize.getEntity();
    params.FKId = entity.fkId;
    params.FKFlag = entity.fkFlag;
    params.Token = entity.manager.Token;
    params.StoreId = entity.fkId;
    params.Id = types;
    console.log(params, '======params');
    util.post("finance.cashaccount.modify", params, function (res) {
      res = res.data;
      if (res.Success) {
        util.tips('修改成功');
        if (callback != undefined && typeof (callback) == "function") {
          data.Id = res.Content;
          callback(data);
        }
      } else {
        util.tips("添加失败：" + res.Message);
      }
    }, "3.1", null, false);
  },
  /**
   * 删除帐号
   */
  getAccountDel(data, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Id: data.id
    };
    util.post("finance.cashaccount.delete", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Content);
      }
    }, "3.1");
  },
  /**
   * 实名认证功能-获取短信验证码
   * @param {String} phone 手机号
   */
  sendSmsCode(phone, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      Mobile: phone
    };
    util.post("finance.distributionwithdraw.messageget", param, function (res) {
      res = res.data
      if (callback != undefined && typeof (callback) == "function") {
        callback(res)
      }
    }, "3.0");
  },
  /**
   * 实名认证功能-提交认证
   * @param {String} phone 手机号
   * @param {String} name 真实姓名
   * @param {String} idNum 身份证号
   * @param {String} smsCode 短信验证码
   */
  uploadRealNameVerify(phone, name, idNum, smsCode, callback) {
    var entity = authorize.getEntity();
    let openid = getStorageSync('openid') // 认证页面一定时已登录的，所以直接从缓存中取openid
    var param = {
      Token: entity.manager.Token,
      Mobile: phone,
      RealName: name,
      IdNo: idNum,
      messageCode: smsCode,
      OpenId: openid,
      ProprietorId: app.dns.proprietorId,
    };
    util.post("finance.distributionwithdraw.save", param, function (res) {
      res = res.data
      if (callback != undefined && typeof (callback) == "function") {
        callback(res)
      }
    }, "3.0");
  },
  /**
   * 实名认证功能-获取审核状态
   */
  getRealNameVerifyStatus(callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
    };
    util.post("finance.distributionwithdraw.realget", param, function (res) {
      res = res.data
      if (callback != undefined && typeof (callback) == "function") {
        callback(res)
      }
    }, "3.0");
  },
}
/**地址的操作 */
const address = {
  load_area: (data, callback) => {
    let param = {
      ParentId: data.parentId || data.id || '',
      Layer: data.layer || ''
    }
    util.post("core.region.get", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
        }
      }
    }, "2.0", null, false);
  },
  /**默认地址 */
  getDefaultAddress(callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId,
      Proprietor: app.dns.proprietor
    };

    util.post("store.address.getdefaultaddress", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Success && res.Content.length > 0 ? res.Content[0] : undefined);
      }
    }, "3.0", null, false);
  },
  getAddressByName(d, callback) {
    this.loadUserAddressList('', (content) => {
      if (content == undefined || content.length == 0) {
        callback();
        return;
      }
      let obj = null;
      if (d && Object.keys(d).length > 0) {
        obj = content.filter(item => item.Province == d.Province && item.Area == d.Area);
        if (obj && obj.length > 0) {
          obj = obj[0];
        }
      }

      //  if(obj==undefined||Object.keys(obj).length==0)
      //  {
      //    obj = content.filter(item => item.IsDefault == 1);
      //  }
      //  if(obj&&obj.length>0){
      //    obj = obj[0];
      //  }
      callback(obj)
    })
  },
  /**地址列表 */
  loadUserAddressList(keyword, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();
    var param = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId,
      Proprietor: app.dns.proprietor,
      keyword: keyword
    };
    var key = "addresslist_cookie_" + entity.fkId + "_" + entity.fkFlag + "_";
    // var obj = storage.get(key);
    // if (obj && obj != undefined && obj != "" && callback && typeof (callback) == "function") {
    //   callback(obj);
    //   return;
    // }
    util.post("store.address.list", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
          if (res.Content.length > 0) {
            storage.put(key, res.Content);
          }

        }
      }
    }, "3.0", null, false, 2);
  },
  /**查找地址 */
  findAdress(adrId, callback) {
    var entity = authorize.getEntity();
    var param = {
      Id: adrId,
      ProprietorId: app.dns.proprietorId,
      Token: entity && entity.manager ? entity.manager.Token : '',
    };

    util.post("store.address.get", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res.Content);
      }
    }, "3.0");
  },
  /**添加地址 */
  addAddress(data, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId,
      Proprietor: app.dns.proprietor,
      FKFlag: app.dns.proprietor,
      FKId: app.dns.proprietorId,
      Name: data.Name,
      Mobile: data.Mobile,
      Province: data.Province,
      City: data.City,
      Area: data.Area,
      Address: data.Address,
      IsDefault: data.IsDefault,
      Town: data.Town || '', // 街道名
      TownId: data.TownId || '', // 街道codeId
    };
    var that = this;
    var key = "addresslist_cookie_" + entity.fkId + "_" + entity.fkFlag + "_";
    util.post("store.address.add", param, function (res) {
      res = res.data;
      if (res.Success) {
        util.success("新增地址成功");
        storage.remove(key);
        if (callback != undefined && typeof (callback) == "function") {
          callback(true);
        }
      } else {
        util.alert("地址添加失败：" + res.Message);
        if (callback != undefined && typeof (callback) == "function") {
          callback(false);
        }
      }
    }, "3.0", null, false);
  },
  /**修改地址 */
  modifyAddress(data, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId,
      Proprietor: app.dns.proprietor,
      FKFlag: app.dns.proprietor,
      FKId: app.dns.proprietorId,
      Name: data.Name,
      Mobile: data.Mobile,
      Province: data.Province,
      City: data.City,
      Area: data.Area,
      Address: data.Address,
      Id: data.Id,
      IsDefault: data.IsDefault,
      Town: data.Town || '', // 街道名
      TownId: data.TownId || '', // 街道codeId
    };
    var that = this;
    var key = "addresslist_cookie_" + entity.fkId + "_" + entity.fkFlag + "_";
    util.post("store.address.modify", param, function (res) {
      res = res.data;
      if (res.Success) {
        util.success("修改地址成功");
        storage.remove(key);
        if (callback != undefined && typeof (callback) == "function") {
          callback(true);
        }
      } else {
        util.alert("地址修改失败：" + res.Message);
        if (callback != undefined && typeof (callback) == "function") {
          callback(false);
        }
      }
    }, "3.0", null, false);
  },
  /**删除地址 */
  deleteAddress(id, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity && entity.manager ? entity.manager.Token : '',
      ProprietorId: app.dns.proprietorId,
      Proprietor: app.dns.proprietor,
      Id: id
    };
    var key = "addresslist_cookie_" + entity.fkId + "_" + entity.fkFlag + "_";
    util.post("store.address.delete", param, function (res) {
      res = res.data;
      storage.remove(key);
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          util.success("删除地址成功");
          callback(res);
        }
      } else {
        util.tips("地址删除失败" + res.Message);
      }
    }, "3.0", null, false);
  },
  //
  addressParse(data, callback) {
    var entity = authorize.getEntity();
    var param = {
      Token: entity.manager.Token,
      address: data.address,
      parse: data.parse,
      ProprietorId: app.dns.proprietorId
    };
    util.post("social.address.parse", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res);
        }
      } else {
        util.alert(res.Message);
      }
    }, "3.1", null, false);
  },
  // 获取自提点列表接口
  selfAddressList: function (data, callback) {
    var entity = authorize.getEntity();
    var param = {
      ProprietorId: app.dns.proprietorId,
      Proprietor: app.dns.proprietor,
      lng: data.lng,
      lat: data.lat,
      PageIndex: data.PageIndex,
      PageSize: data.PageSize
    };
    util.post("annex.pickpoint.page", param, function (res) {
      res = res.data;
      if (res.Success) {
        if (callback != undefined && typeof (callback) == "function") {
          callback(res);
        }
      } else {
        util.alert(res.Message);
      }
    }, "3.0", null, false);
  },
}
/**完善信息操作 */
const perfect = {
  get_setting: function (data, callback) {
    let param = {
      profileToken: data,
      ProprietorId: app.dns.proprietorId
    }
    util.post("credit.profile.setting", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.0");
  },
  save: function (data, callback) {
    let param = {
      Customize: data.Customize,
      profileToken: data.profileToken,
      ProprietorId: app.dns.proprietorId
    };
    util.post("credit.profile.save", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.0");
  },
  reapply: function (data, callback) {
    let param = {
      profileToken: data,
      ProprietorId: app.dns.proprietorId
    };
    util.post("credit.profile.reapply", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        callback(res);
      }
    }, "3.0");
  },
  getBusinessItem: function (data, callback) {
    util.post("store.business.scope", data, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
        } else {
          util.tips(res.Message);
        }
      }
    }, "3.0");
  }
}
/**企业微信相关 */
const qywx = {
  getFollow(callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      StoreId: entity.fkId,
      ProprietorId: app.dns.proprietorId
    };
    util.post("wxgroup.store.follow.get", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
        }
      }
    }, "3.0", null, false, 0, false);
  },
  showlink(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      StoreId: entity.fkId,
      page: data,
      ProprietorId: app.dns.proprietorId
    };
    util.post("wxgroup.store.customer.isshowlinkmanbutton", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
        }
      }
    }, "3.0", null, false, 0, false);
  }
}
/**发票相关 */
const invoice = {
  invoicePageList(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      PageIndex: data.pageIndex || 1,
      PageSize: data.pageSize || 20,
      Keyword: data.keyword || ''
    };
    util.post("vast.finance.invoice.page", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
        }
      }
    }, "3.1", null, false, 0, false);
  },
  invoiceList(invType, keyword, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      Keyword: keyword || ''
    };
    if (invType > 0) {
      param.InvType = invType;
    }
    util.post("vast.finance.invoice.list", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(res.Content);
        }
      }
    }, "3.1", null, false, 0, false);
  },
  invoiceInfo(id, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      Id: id || 0
    };
    util.post("vast.finance.invoice.get", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(true, res.Content);
        } else {
          callback(false, res.Message);
        }
      }
    }, "3.1", null, false, 0, false);
  },
  saveInvoice(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      ...data
    };
    util.post("vast.finance.invoice.save", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(true, res.Content);
        } else {
          callback(false, res.Message);
        }
      }
    }, "3.1", null, false, 0, false);
  },
  setInvoiceDefault(data, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      Id: data.id || 0,
      IsDefault: data.isDefault
    };
    util.post("vast.finance.invoice.default", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(true, res.Content);
        } else {
          callback(false, res.Message);
        }
      }
    }, "3.1", null, false, 0, false);
  },
  deleteInvoice(id, callback) {
    var entity = authorize.getEntity() || authorize.getDefault();;
    var param = {
      Token: entity.manager.Token,
      Id: id || 0
    };
    util.post("vast.finance.invoice.del", param, function (res) {
      res = res.data;
      if (callback != undefined && typeof (callback) == "function") {
        if (res.Success) {
          callback(true, res.Content);
        } else {
          callback(false, res.Message);
        }
      }
    }, "3.1", null, false, 0, false);
  },
}

let searchHistory = {
  getData() {
    var entity = authorize.getEntity() || authorize.getDefault()
    var manager = entity.manager || {}
    var param = {
      Token: manager.Token,
      FKFlag: entity.proprietor,
      FKId: entity.proprietorId,
      UserId: manager.ManagerId || ''
    }
    return new Promise((resolve) => {
      util.post("vast.historykeysearch.get.list", param, function (res) {
        res = res.data
        if (res.Success) {
          resolve(res)
        }
      }, "3.0", null, false, 0, false);
    })
  },
  add (Keyword = '') {
    var entity = authorize.getEntity() || authorize.getDefault()
    var manager = entity.manager || {}
    var param = {
      Token: manager.Token,
      FKFlag: entity.proprietor,
      FKId: entity.proprietorId,
      UserId: manager.ManagerId,
      Keyword
    }
    return new Promise((resolve) => {
      util.post("vast.historykeysearch.add.keyword", param, function (res) {
        res = res.data
        if (res.Success) {
          resolve(true, res.Content)
        } else {
          resolve(false, res.Message)
        }
      }, "3.0", null, false, 0, false);
    })
  },
  remove (isAll = false, id = '') {
    var entity = authorize.getEntity() || authorize.getDefault()
    var manager = entity.manager || {}
    var param = {
      Token: manager.Token,
      FKFlag: entity.proprietor,
      FKId: entity.proprietorId,
      UserId: manager.ManagerId
    }
    if (isAll) {
      param.IsALL = 1
    } else {
      param.IsALL = 0
      param.id = id
    }
    return new Promise((resolve) => {
      util.post("vast.historykeysearch.delete.keyword", param, function (res) {
        res = res.data
        if (res.Success) {
          resolve(true, res.Content)
        } else {
          resolve(false, res.Message)
        }
      }, "3.0", null, false, 0, false);
    })
  },
}

export {
  profile,
  address,
  finance,
  perfect,
  qywx,
  invoice,
  searchHistory
}